package com.tonglin.eduservice.service.impl;

import com.tonglin.eduservice.entity.EduCourse;
import com.tonglin.eduservice.entity.EduCourseDescription;
import com.tonglin.eduservice.entity.vo.CourseInfoVo;
import com.tonglin.eduservice.entity.vo.CoursePublishVo;
import com.tonglin.eduservice.mapper.EduCourseMapper;
import com.tonglin.eduservice.service.EduChapterService;
import com.tonglin.eduservice.service.EduCourseDescriptionService;
import com.tonglin.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tonglin.eduservice.service.EduVideoService;
import com.tonglin.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author tonglin
 * @since 2021-05-10
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService courseDescriptionService;
    @Autowired
    EduCourseMapper eduCourseMapper;
    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    EduChapterService eduChapterService;
    @Autowired
    EduCourseDescriptionService descriptionService;



    //添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert==0){
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        //获取添加之后课程id
        String cid = eduCourse.getId();
        System.out.println("------我是cid"+cid);
        //2.向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);
        return cid;
    }
    //根据课程id查询课程信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        System.out.println("--------我是id"+courseId);
        //先根据课程id获取课程
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //根据课程id获取课程描述
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        System.out.println(eduCourseDescription.getDescription()+"-----我是描述！！！！");
        return courseInfoVo;
    }
    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //给课程表传信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update==0){
            throw new GuliException(20001,"修改课程失败");
        }

        //给课程描述表传信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(eduCourseDescription);

    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getPublishCourseInfo(id);

        return coursePublishVo;
    }

    @Override
    public void removeCourseById(String courseId) {
        //删除小节
        eduVideoService.deleteVideo(courseId);
        //删除章节
        eduChapterService.deleteChapterByCourseId(courseId);
        //删除描述
        courseDescriptionService.delete(courseId);
        //删除课程
        int delete = baseMapper.deleteById(courseId);
        if (delete == 0){
            throw new GuliException(20001,"删除失败");
        }
    }


}
